package com.qys.livesMall.aspect;


import com.qys.livesMall.app.user.model.entity.LimitUrl;
import com.qys.livesMall.app.user.model.pojo.LimitCachePOJO;
import com.qys.livesMall.app.user.service.LimitService;
import com.qys.livesMall.app.user.service.LimitUrlService;
import com.qys.livesMall.cache.RedisCache;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.utils.IpUtils;
import com.qys.livesMall.common.utils.MessageUtils;
import com.qys.livesMall.common.utils.ServletUtils;
import com.qys.livesMall.framework.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.qys.livesMall.app.user.constants.RedisConstant.LIMIT_URLS;
import static com.qys.livesMall.app.user.constants.RedisConstant.LIMIT_USER;

@Slf4j
@Aspect
@Component
@EnableAspectJAutoProxy
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppUserLimitAspect {


    private final RedisCache redisCache;
    private final LimitService limitService;


    @Pointcut("within(com.qys.livesMall..controller.app..*)")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void doBefore(JoinPoint point) {
        /**判断缓存是否有现在地址*/
        List<LimitUrl> limitUrlList = redisCache.getCacheObject(LIMIT_URLS);
        if (limitUrlList == null || limitUrlList.size() == 0) {
            return;
        }
        /**获取请求地址*/
        String requestURI = ServletUtils.getRequest().getRequestURI();
        log.info("收到App请求地址-----------"+requestURI);
        LimitUrl limitUrl = null;

        for (LimitUrl url:limitUrlList){
            if (url.getUri().substring(url.getUri().lastIndexOf("/")).contains("*")){
                if (requestURI.contains(url.getUri().substring(0,url.getUri().lastIndexOf("/")))){//匹配地址
                    limitUrl = url;
                    break;
                }
            }else {
                if (url.getUri().equals(requestURI)) {//匹配地址
                    limitUrl = url;
                    break;
                }
            }
        }
        /**不存在匹配*/
        if (null == limitUrl) {
            return;
        }
        /**查询权限*/
        List<LimitCachePOJO> cacheList = redisCache.getCacheObject(LIMIT_USER + SecurityUtils.getUserId());
        if (cacheList == null || cacheList.size() == 0) {
            limitService.processLimit(SecurityUtils.getUserId());
            return;
        }
        LimitUrl finalLimitUrl = limitUrl;
        cacheList.forEach(e->{
           if (e.getLimitUrlId().equals(finalLimitUrl.getId())){
               if (e.getStatus()==2){
                   throw new QysException(MessageUtils.message("已禁止该操作,请联系管理员"));
               }
           }
        });
    }
}
